package Persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

public class poolConnection {

private Vector <Connection> connections = new Vector<Connection>();
private static poolConnection pool;
private String jdbc, servidor, usuario, password;
public int cantCon;

private poolConnection()
{
	setVariables();
	for (int i= 0; i< cantCon;i++)
		connections.add(connect());
}

public static poolConnection getPoolConnection()
{
	if (pool== null)
		pool =new poolConnection();
	return pool;
}

private void setVariables(){
    jdbc = "";
    servidor = "";
    usuario = "";
    password = "";
    cantCon = 3;

}

private Connection connect()
{
	try
	{
		//Setear driver
		Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance ();
        String dbConnectString = jdbc + servidor; 
        Connection con = DriverManager.getConnection (dbConnectString, usuario, password);
        
        return con;
	}
	catch (SQLException e)
	{
		System.out.println("Mensaje Error: " + e.getMessage());
		System.out.println("Stack Trace: " + e.getStackTrace());
		return null;
	}
	catch (Exception ex)
	{
		System.out.println("Mensaje Error: " + ex.getMessage());
		System.out.println("Stack Trace: " + ex.getStackTrace());
		return null;
	}
}

	public  Connection getConnection()
	{
		Connection c = null;
		if (connections.size()>0)
			c = connections.remove(0);
		else
		{
			c = connect();
			System.out.println("Se ha creado una nueva conexion fuera de los parametros de configuracion");
		}
		return c;
	}
	public void realeaseConnection(Connection c)
	{
		connections.add(c);
	}
}
